Interrupting `while loop` with keyboard in Cython
        Posted  
        
            by 
                linello
            
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by linello
        
        
        
        Published on 2012-11-12T10:22:00Z
        Indexed on 
            2012/11/12
            11:00 UTC
        
        
        Read the original article
        Hit count: 276
        
I want to be able to interrupt a long function with cython, using the usual CTRL+C interrupt command. My C++ long function is repeatedly called inside a while loop from Cython code, but I want to be able, during the loop, to send an "interrupt" and block the while loop.
The interrupt also should wait the longFunction() to finish, so that no data are lost or kept in unknown status.
This is one of my first implementation, which obviously doesn't work:
computed=0;
print "Computing long function..."
    while ( computed==0 ):
        try:
            computed = self.thisptr.aLongFunction()
        except (KeyboardInterrupt, SystemExit):
            computed=1
            print '\n! Received keyboard interrupt.\n'
            break;
(p.s. self.thisptr is the pointer to the current class which implements aLongFunction() )
© Stack Overflow or respective owner